<template>
	<view class="content">
		<!-- 		<view class="top-group-view">
			<view class="top-title-view" :style="'height:'+navigationBarHeight+'px;margin-top:'+iStatusBarHeight+'px;'">
				<text class="top-title">去水印</text>
			</view>
		</view> -->
		
		<view class="s-btns">
			<view class="s-btn" @click="changeType"  :class="{sel:type==0}">去水印</view>
			<view class="s-btn" @click="changeType" :class="{sel:type==1}">违禁词</view>
		</view>

		<view class="link-wrap">
			<textarea class="link-content" v-model="link" :placeholder="type==0?'粘贴视频分享链接放这里':'粘贴文案放这里'" cols="30" rows="5">

			</textarea>
			<view class="btns">
				<view @click="paste" class="btn green">
					粘贴链接
				</view>
				<view class="btn orange" @click="clear">
					清空内容
				</view>
				<view class="btn blue" v-if="type==0" @click="parse">
					解析视频
				</view>
				<view class="btn blue" v-else @click="parseText">
					立即检测
				</view>
			</view>
		</view>
		
		<view style="padding: 0 40rpx;" class="dy-text" v-if="textInfo">
			<view style="color: red;margin-top: 10rpx;" class="text-name" v-for="name in Object.keys(textInfo).reverse()">
				{{name}}:
				<text style="margin-left: 10rpx;" v-for="word in textInfo[name]">{{word}}</text>
				
			</view>
		</view>

		<view class="video-wrap" v-if="videoInfo">
			<view class="btns">
				<view @click="copy" class="btn green">
					复制链接
				</view>
				<view class="btn orange" @click="downVideo">
					保存视频
				</view>
				<!-- 		<view class="btn blue" @click="parse">
					保存文案
				</view> -->
				<view class="btn red" @click="copyText">
					复制文案
				</view>
			</view>
		


			<video v-if="videoInfo.videoUrl" class="video" :src="videoInfo.videoUrl"></video>
			<view v-if="videoInfo.images">
				<view class="img-info" @click="saveAllImg">点我一键保存所有图片</view>
				<view class="img-info" style="font-size: 20rpx;">无法一键保存时,长按下面图片保存</view>
				<view class="img-wrap" v-for="(item,index) in videoInfo.images" :key="index">
					<view class="img-info" style="font-size: 20rpx;">图{{index+1}}(点击查看详情)</view>
					<image @click="showPreImg(index)" mode="aspectFill" show-menu-by-longpress class="dy-img"
						:src="item"></image>
				</view>

			</view>

			<view class="img-wrap" v-else>
				<view class="img-info">长按下面图片保存</view>
				<image show-menu-by-longpress class="dy-img" :src="videoInfo.imgUrl"></image>
			</view>



		</view>



	</view>
</template>

<script>
	import configUrls from '@/common/config.js';
	export default {
		data() {
			return {
				type:0,
				link: '',
				videoInfo: null,
				textInfo: null,
				iStatusBarHeight: 0,
				navigationBarHeight: 0
			}
		},
		onLoad() {
			this.isLogin = uni.getStorageSync("isLogin");
			if (!this.isLogin) {
				uni.reLaunch({
					url: '/pages/login/wxlogin'
				})
			}
		},
		onReady() {
			this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			const customHeight = wx.getMenuButtonBoundingClientRect();
			this.navigationBarHeight = customHeight.height + (customHeight.top - this.iStatusBarHeight) * 2;
		},
		methods: {
			changeType(){
					this.link = ''
					this.videoInfo = null
					this.textInfo = null
					if(this.type==0){
						this.type=1
					}else{
						this.type = 0
					}
			},
			saveAllImg() {
				uni.getSetting({
					success: (res) => {
						console.log(res);
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success(res) {
									this.downImg(0);
								},
								fail() {
									uni.showModal({
										content: '请允许相册权限, 拒绝将无法正常保存视频',
										showCancel: false,
										success() {
											uni.openSetting({
												success(settingdata) {
													if (settingdata.authSetting[
															'scope.writePhotosAlbum'
														]) {} else {
														console.log('获取权限失败')
														uni.showToast({
															title: "获取权限失败",
															icon: 'none',
															duration: 2000
														});
													}
												}
											})
										}
									})
								}
							})
						} else {
							this.downImg(0);
						}
					}
				})
			},
			showPreImg(index) {
				uni.previewImage({
					urls: this.videoInfo.images,
					current: index
				})
			},
			downVideo() { //保存图片到相册按钮的点击事件函数
				this.downUrl(this.videoInfo.videoUrl)
			},
			downImg(index) {
				let length = this.videoInfo.images.length
				uni.showLoading({
					title: `下载图${index+1}/${length}`
				})
				let that = this;
				let fileName = new Date().valueOf();
				let filePath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpeg'
				const task = uni.downloadFile({
					url: this.videoInfo.images[index],
					filePath: filePath, //  拼接本地文件路径
					success: (res) => {
						let filePath = res.filePath;
						uni.saveImageToPhotosAlbum({
							filePath,
							success: (res) => {
								if (index + 1 == length) {
									uni.showToast({
										title: '全部下载成功',
										icon: 'none',
										duration: 2000
									})
								}
								let fileMgr = wx.getFileSystemManager();
								// 删除本地文件
								fileMgr.unlink({
									filePath: filePath,
									success: function(r) {
										console.log(r)
									},
								})
								uni.hideLoading()
								if (index + 1 < length) {
									this.downImg(index + 1)
								}
							},
							fail(err) {
								uni.hideLoading()
								uni.showToast({
									title: '保存失败',
									icon: 'none',
									duration: 2000
								})
							},
							complete(res) {
								console.log(res)
								// uni.hideLoading()
							}
						})
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							title: '下载失败,请稍后再试',
							icon: 'none',
						})
					},
					complete(res) {
						console.log(res)
					}
				})

			},
			downloadFileClick(url) {
				uni.showLoading({
					mask: true,
					title: '下载中...'
				})
				let that = this;
				let fileName = new Date().valueOf();
				const task = uni.downloadFile({
					url: url,
					filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4', //  拼接本地文件路径
					success: (res) => {
						let filePath = res.filePath;
						uni.saveVideoToPhotosAlbum({
							filePath,
							success: (res) => {
								uni.showToast({
									title: '下载成功',
									icon: 'none',
									duration: 2000
								})
								let fileMgr = wx.getFileSystemManager();
								// 删除本地文件
								fileMgr.unlink({
									filePath: wx.env.USER_DATA_PATH + '/' + fileName +
										'.mp4',
									success: function(r) {
										console.log(r)
									},
								})
							},
							fail(err) {
								uni.showToast({
									title: '保存失败',
									icon: 'none',
									duration: 2000
								})
							},
							complete(res) {
								console.log(res)
								uni.hideLoading()
							}
						})
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							title: '下载失败,请稍后再试',
							icon: 'none',
						})
					},
					complete(res) {
						console.log(res)
					}
				})
				task.onProgressUpdate(this.onProgress)
			},
			onProgress(res) {
				uni.showLoading({
					mask: true,
					title: res.progress ? '下载中' + res.progress + "%" : '下载中...'
				})
			},
			downUrl(downLoadUrl) {
				if (!downLoadUrl) {
					retrun
				}
				let that = this
				uni.getSetting({
					success(res) {
						console.log(res);
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success(res) {
									that.downloadFileClick(downLoadUrl);
								},
								fail() {
									uni.showModal({
										content: '请允许相册权限, 拒绝将无法正常保存视频',
										showCancel: false,
										success() {
											uni.openSetting({
												success(settingdata) {
													if (settingdata.authSetting[
															'scope.writePhotosAlbum'
														]) {} else {
														console.log('获取权限失败')
														uni.showToast({
															title: "获取权限失败",
															icon: 'none',
															duration: 2000
														});
													}
												}
											})
										}
									})
								}
							})
						} else {
							that.downloadFileClick(downLoadUrl)
						}
					}
				})
			},
			copyText() {
				if (this.videoInfo?.title) {
					uni.setClipboardData({
						data: this.videoInfo?.title
					})
				}
			},
			copy() {
				if (this.videoInfo?.videoUrl) {
					uni.setClipboardData({
						data: this.videoInfo?.videoUrl
					})
				}
			},
			parseText() {
				if (!this.link) {
					return
				}
				let params = {
					text: this.link
				}
				this.$Request.postParamsLoadView(configUrls.parseDyText, params).then(res => {
					console.log(res)
					if (res.code == "10000") {
						this.textInfo = res.data
					} else {
					
						if(res.msg&&res.msg.indexOf("授权")>-1){
							uni.showModal({
								title: "抖音号未授权!",
								content: "抖音授权后才能使用",
								confirmText: '去授权',
								success(res) {
									if (res) {
										uni.navigateTo({
											url: "/pages/mine/manage"
										})
									} else {
										console.log('取消')
									}
							
								},
								fail() {
									console.log('取消')
								}
							})
						}else{
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 2000
							})
						}
					
					}
				
				})
			},
			parse() {
				if (!this.link) {
					return
				}
				let params = {
					link: this.link,
					type: 0
				}
				this.$Request.postParamsLoadView(configUrls.parseDyVideo, params).then(res => {
					console.log(res)
					if (res.code == "10000") {
						this.videoInfo = res.data
					} else {
					
						if(res.msg&&res.msg.indexOf("授权")>-1){
							uni.showModal({
								title: "抖音号未授权!",
								content: "抖音授权后才能使用",
								confirmText: '去授权',
								success(res) {
									if (res) {
										uni.navigateTo({
											url: "/pages/mine/manage"
										})
									} else {
										console.log('取消')
									}
							
								},
								fail() {
									console.log('取消')
								}
							})
						}else{
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 2000
							})
						}
					
					}

				})
			},
			clear() {
				this.link = ""
			},
			paste() {
				uni.getClipboardData({
					success: (res) => {
						this.link = res.data
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background: linear-gradient(90deg, #FEFDF3, #EEF1CF);
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
	}

	.top-group-view {
		width: 100vw;
		display: flex;
		flex-direction: column;
		background: linear-gradient(90deg, #FEFDF3, #EEF1CF);
	}

	.top-title-view {
		display: flex;
		align-items: center;
	}

	.top-title {
		font-size: 32rpx;
		font-family: MiSans-Medium, MiSans;
		font-weight: bold;
		margin-left: 32rpx;
		color: #333333;
	}

	.link-wrap {
		padding: 40rpx 40rpx;
		text-align: left;
	}

	.link-content {
		width: auto;
		height: 200rpx;
		padding: 30rpx 30rpx;
		margin: 0 auto;
		border: 1rpx solid red;
	}

	.btns {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
		margin-top: 10rpx;
	}

	.btn {
		text-align: center;
		flex: 1;
		color: white;
		padding: 15rpx 0;
		border-radius: 10rpx;
		font-size: 35rpx;
	}

	.btn.orange {
		background-color: orange;
	}

	.btn.red {
		background-color: red;
	}

	.btn.blue {
		background: linear-gradient(to right, #b221ff, #14b7ff);
	}

	.btn.green {
		background: #449d44;
	}
	

	.video-wrap {
		padding: 40rpx 40rpx;
	}

	.video {
		margin-top: 20rpx;
		width: 100%;
	}

	.img-wrap .dy-img {
		width: 100%;
	}

	.img-info {
		text-align: center;
		color: red;
		margin: 20rpx 0;
	}
	.s-btns{
		margin-top: 20rpx;
		padding: 20rpx 40rpx;
		display: flex;
		justify-content: space-between;
	}
	.s-btn{
		color: brown;
		width: 200rpx;
		text-align: center;
	}
	.s-btn.sel{
		color:red;
		border:1rpx solid white;
	}
</style>